function out  = frameRec(coeff, dlevel, rlevel ,HR0)
%This program performs 1D framelet reconstruction
%parameters:
%coeff : the input transform coefficients. Type : cell
%dlevel : the level of decomposition of the orignal signal. Type : int
%rlevel : to which level the program perform reconstructoin. Type : int
%HR0 : the reconstruction filter. Type : cell
%output:
%out : the reconstruction result . Type : cell.

nfilters = numel(HR0);
lenSignal = length(coeff)/((nfilters-1)*dlevel+1);
out = [];
HR{1} = HR0;
for i = 2 : dlevel
    HR{i} = doubleFilter(HR{i-1});
end

currentLevel = dlevel ;
while rlevel < dlevel
    lenFilter = length(HR{currentLevel}{1});
    out1 = zeros(1,lenSignal);
    
    for k = 1 : nfilters
        tmpSig = coeff((k-1)*lenSignal+1 : k*lenSignal);
        extSignal = [tmpSig(lenSignal - (lenFilter-3)/2 : lenSignal) tmpSig tmpSig(1:(lenFilter-1)/2)];
        out1 = out1 + conv(extSignal, HR{currentLevel}{k},'valid');
    end
    
    coeff(1 : nfilters*lenSignal) = [];
    coeff = [out1 coeff];
 
    rlevel = rlevel + 1;
    currentLevel = currentLevel -1;
    clear filters;
    
end
out = coeff;
end

